package engine.systems.audio.audio
{
	import ash.core.Component;
	
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.utils.Dictionary;
	
	public class Audio extends Component
	{
		public static const AUDIO_PATH:String 	= "audio/";
		public static const MUSIC_PATH:String 	= "music/";
		public static const EFFECTS_PATH:String = "effects/";
		
		private static var cache:Dictionary = new Dictionary();
		
		internal var _toPlay:Vector.<AudioData> = new Vector.<AudioData>();
		internal var _toStop:Vector.<AudioData> = new Vector.<AudioData>();
		internal var _toFade:Vector.<AudioData> = new Vector.<AudioData>();
		
		internal var _playing:Vector.<AudioData> 	= new Vector.<AudioData>();
		internal var _fading:Vector.<AudioData> 	= new Vector.<AudioData>();
		
		public function Audio()
		{
			super();
		}
		
		override public function destroy():void
		{
			
		}
		
		public function play(url:String, name:String = null, loop:Boolean = false):void
		{
			this._toPlay.push(new AudioData(url, name, loop));
		}
		
		public function stopByURL(url:String):void
		{
			this.stop("url", url);
		}
		
		public function stopByName(name:String):void
		{
			this.stop("name", name);
		}
		
		private function soundLoaded(sound:Sound):void
		{
			
		}
		
		private function stop(property:String, string:String):void
		{
			var i:int;
			var length:int;
			var data:AudioData;
			
			length = this._toPlay.length;
			for(i = 0; i < length; i++)
			{
				data = this._toPlay[i];
				if(data[property] == string)
				{
					this._toStop.push(data);
					
					data.loop = false;
					this._toPlay.splice(i, 1);
					i--;
				}
			}
			
			length = this._toFade.length;
			for(i = 0; i < length; i++)
			{
				data = this._toFade[i];
				if(data[property] == string)
				{
					this._toStop.push(data);
					
					data.loop = false;
					this._toFade.splice(i, 1);
					i--;
				}
			}
			
			length = this._playing.length;
			for(i = 0; i < length; i++)
			{
				data = this._playing[i];
				if(data[property] == string)
				{
					this._toStop.push(data);
					
					data.loop = false;
					this._playing.splice(i, 1);
					i--;
				}
			}
			
			length = this._fading.length;
			for(i = 0; i < length; i++)
			{
				data = this._fading[i];
				if(data[property] == string)
				{
					this._toStop.push(data);
					
					data.loop = false;
					this._fading.splice(i, 1);
					i--;
				}
			}
		}
		
		public function fadeByURL():void
		{
			
		}
	}
}